//
//  PSCell.m
//  ParallaxScrollingDemo
//
//  Created by 余意 on 2016/12/16.
//  Copyright © 2016年 余意. All rights reserved.
//

#import "PSCell.h"

@implementation PSCell

- (void)awakeFromNib
{
    [super awakeFromNib]; 
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self setup];
    }
    return self;
}

- (void)setup
{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.clipsToBounds = YES;
    
    UIView * line = [UIView new];
    line.backgroundColor = [UIColor lightGrayColor];
    line.frame = CGRectMake(0, PARALLAXCELL_HEIGHT - 1, SCREEN_WIDTH, 1);
    [self.contentView addSubview:line];
}

- (void)cancelAnimation
{
    [self.backIV.layer removeAllAnimations];
}

- (void)celloffset
{
    // 1、获取cell在屏幕中的rect
    CGRect  centerToWindow = [self convertRect:self.bounds toView:self.window];
    // 2、获取cell中心点y轴坐标
    CGFloat centerY        = CGRectGetMidY(centerToWindow);
    // 3、获取cell父视图的中心点
    CGPoint windowCenter   = self.superview.center;
    // 4、获取距离差
    CGFloat cellOffsetY = centerY - windowCenter.y;
    // 5、距离差 / 2倍父视图高度
    CGFloat offsetDig =  cellOffsetY / self.superview.frame.size.height * 2;
    // 6、计算偏移 kScreenHeight * 0.5 为图片视图的高度
    CGFloat offset    =  - offsetDig * (SCREEN_HEIGHT * 0.5 - PARALLAXCELL_HEIGHT) / 2;
    
    CGAffineTransform transY   = CGAffineTransformMakeTranslation(0, offset);
    self.backIV.transform = transY;
}

- (UIImageView *)backIV
{
    if (!_backIV)
    {
        _backIV = [UIImageView new];
        _backIV.frame = CGRectMake(0, -(SCREEN_HEIGHT * 0.5 - PARALLAXCELL_HEIGHT) / 2, SCREEN_WIDTH, SCREEN_HEIGHT * 0.5);
        [self.contentView addSubview:_backIV];
    }
    return _backIV;
}

- (UILabel *)titleLab
{
    if (!_titleLab)
    {
        _titleLab = [UILabel new];
        _titleLab.frame = CGRectMake(0, 0, SCREEN_WIDTH, 45);
        _titleLab.center = self.contentView.center;
        _titleLab.font = [UIFont systemFontOfSize:25];
        _titleLab.textColor = [UIColor whiteColor];
        _titleLab.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_titleLab];
    }
    return _titleLab;
}



@end
